const express = require('express')

const app = express()

app.use(express.urlencoded({ extended: false }))
app.use(express.json())

const nunjucks = require('nunjucks')
nunjucks.configure(`${__dirname}/views`, {
  autoescape: true
})

// 页面
app.get('/basics', (_, res) => {
  res.send(nunjucks.render('基础使用.html'))
})

app.get('/alias', (_, res) => {
  res.send(nunjucks.render('别名使用.html'))
})

// 接口
app.get('/queryUser', (req, res) => {
  const {
    id
  } = req.query
  res.json({
    code: '10087',
    msg: '查询成功',
    data: {
      id,
      username: 'Tom',
      age: '18'
    }
  })
})

app.post('/insertUser', (req, res) => {
  const { username, password, tel } = req.body
  res.json({
    code: '10086',
    msg: '添加成功'
  })
  console.log(username, password, tel)
})

app.put('/updateUser', (req, res) => {
  const { id, username, password, tel } = req.body
  res.json({
    code: '10086',
    msg: '更新成功',
    data: {
      id
    }
  })
  console.log(username, password, tel)
})

app.delete('/deleteUser', (req, res) => {
  const { id } = req.query
  res.json({
    code: '10087',
    msg: '删除成功',
  })
})

app.listen(80, () => {
  console.log('express server running at http://127.0.0.1');
})